

import java.net.*;  // for Socket
import java.io.*;   // for IOException and Input/OutputStream
import java.util.logging.Level;
import java.util.logging.Logger;

public class Client extends Thread {
    private String server="";
    int servPort = 5441;
    private OutputStream out;
    private Socket socket;
    private InputStream in;

    Client(String otherPlayersComputerIP) throws IOException {
        this.server=otherPlayersComputerIP;
        // Create socket that is connected to server on specified port
        //this.socket = new Socket(server, servPort);
        //this.out = this.socket.getOutputStream();
    }

    Client() {
        
    }

    public void setServerToConnectTo(String otherPlayersComputerIP) {
        this.server = otherPlayersComputerIP;
    }

    public void setServerPortToConnectTo(int port) {
        this.servPort = port;
    }

    public void sendData(String data) throws IOException {
        byte[] byteBuffer = data.getBytes(); // Convert input String to bytes using the default character encoding
        if (data != null && byteBuffer != null && this.out !=null) {
            //System.out.println("Sending: " + data);
            this.out.write(byteBuffer);
        }
    }

    public boolean checkSend() {
        if(this.server == "") {
            return false;
        } else {
            return true;
        }
    }

    public void closeClient() throws IOException {
        this.socket.close();
    }

    public void restart() throws IOException {
        this.socket.close();
        this.socket = new Socket(server, servPort);
        this.out = this.socket.getOutputStream();
    }

    public void run() {
        try {
            this.socket = new Socket(server, servPort);
            this.out = this.socket.getOutputStream();
            this.in = this.socket.getInputStream();
            //in.read to not overflow 
        } catch (IOException ex) {
            Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
        }
  }

    /*
    public static void main(String[] args) throws IOException {
        String server = args[0];       // Server name or IP address
        Client connector = new Client(server);
        //System.out.println("Connected to server...sending echo string");
        connector.sendData(args[1]); // Send the encoded string to the server
        connector.closeClient();
    }
    */
}
